package com.xiaoxie.boot.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.xiaoxie.boot.bean.Menu;
import com.xiaoxie.boot.bean.MenuInfo;
import com.xiaoxie.boot.service.MenuService;
import com.xiaoxie.boot.utils.DataInfo;
import com.xiaoxie.boot.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author xiaoxie
 * @create 2022-03-27 16:05
 */
@Api(tags = "菜单API")
@RestController
@Slf4j
@RequestMapping("/menu")
public class MenuController {
    @Autowired
    MenuService menuService;

    /**
     * 初始化菜单信息
     *
     * @param request
     * @return
     */
    @GetMapping("/init")
    @ApiOperation(value = "初始化菜单信息", notes = "初始化菜单信息")
    @ApiOperationSupport(author = "小谢")
    public Map<String, Object> init(HttpServletRequest request) {
        Integer id = (Integer) request.getSession().getAttribute("id");
        Map<String, Object> init = menuService.init(id);
        return init;
    }

    /**
     * 查询所有菜单信息
     *
     * @return
     */
    @GetMapping("/queryAllMenu")
    @ApiOperation(value = "查询所有菜单信息", notes = "查询所有菜单信息")
    @ApiOperationSupport(author = "小谢")
    public DataInfo queryAllMenu() {
        List<Menu> menus = menuService.queryAllMenu();
        return DataInfo.ok("成功", menus.size(), menus);
    }

    /**
     * 查询所有菜单树
     *
     * @return
     */
    @GetMapping("/initMenu")
    @ApiOperation(value = "查询所有菜单树", notes = "查询所有菜单树")
    @ApiOperationSupport(author = "小谢")
    public DataInfo initMenu() {
        List<MenuInfo> menuInfos = menuService.initMenu();
        return DataInfo.ok("成功", menuInfos.size(), menuInfos);
    }

    /**
     * 删除菜单
     *
     * @param request
     * @param id
     * @return
     */
    @DeleteMapping("/deleteMenu")
    @ApiOperation(value = "删除", notes = "删除菜单")
    @ApiOperationSupport(author = "小谢")
    public DataInfo deleteMenu(Integer id, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String name = (String) session.getAttribute("name");
        log.info(name + "试图删除菜单");
        int i = menuService.deleteMenuById(id);
        if (i == 1) {
            log.info(name + "删除成功");
            return DataInfo.ok();
        } else {
            log.info(name + "删除失败");
            return DataInfo.fail("删除失败");
        }
    }

    /**
     * 修改菜单信息
     *
     * @param menu
     * @param request
     * @return
     */
    @PutMapping("/editMenu")
    @ApiOperation(value = "修改", notes = "修改菜单信息")
    @ApiOperationSupport(author = "小谢")
    public DataInfo editMenu(@RequestBody Menu menu, HttpServletRequest request) {
        HttpSession session = request.getSession();
        String name = (String) session.getAttribute("name");
        log.info(name + "修改了菜单：" + menu.getTitle() + "的信息");
        menu.setUpdateTime(DateUtil.getTimestamp());
        int i = menuService.updateByPrimaryKeySelective(menu);
        if (i == 1) {
            log.info(name + "修改成功");
            return DataInfo.ok();
        } else {
            log.info(name + "修改失败");
            return DataInfo.fail("菜单修改失败!");
        }
    }
}
